<?php
session_start();
$initTitle = "South Salem High School - Courses";
$style="<link href=\"http://www.southsaxons.com/sshs.css\" rel=\"stylesheet\" type=\"text/css\" /><style>\n .opacityFont array(\n filter:alpha(opacity=75);\n -moz-opacity:.75; \n opacity:.75; \n ) \n .opacity array(\n background-color: #FFF;\n filter:alpha(opacity=75);\n -moz-opacity:.75;\n opacity:.75;\n border: 1px solid #000;\n color: #000;\n )\n </style>";
$id = "1";
//$yearactive = 9;
if (isset($changeyear))
{
	$_SESSION["yearactive"] = $changeyear;
	if (isset($_SESSION["selected"]))
	{
		unset($_SESSION["selected"]);
		unset($_SESSION["selecteddep"]);
	}
}
if (isset($_SESSION["yearactive"]))
	$yearactive = $_SESSION["yearactive"];
else
	$yearactive=9;
	
	
if (isset($changesemester))
{
	$_SESSION["semesteractive"] = $changesemester;
	if (isset($_SESSION["selected"]))
	{
		unset($_SESSION["selected"]);
		unset($_SESSION["selecteddep"]);
	}
}
if (isset($_SESSION["semesteractive"]))
	$semesteractive = $_SESSION["semesteractive"];
else
	$semesteractive=1;
	
	
if (isset($dept))
	$sdept = $dept;
	
if (isset($selectdep))
{
	if ($selectdep!="Elective")
		$_SESSION["selected"] = "seq IN (SELECT course_id FROM requirements WHERE Dept = '$selectdep' AND MinGrade='$yearactive')";
	else
		$_SESSION["selected"] = " 1=1";
	$_SESSION["selecteddep"] = $selectdep;
}

echo "<HTML><HEAD><TITLE>$initTitle</TITLE>$style";

$db_host = 'localhost';
$db_user = 'pepgsou_admin';
$db_pwd = '_20pepg09';
$database = 'pepgsou_main';
$table = 'JL';
if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");


if (isset($insertid))
{
	$iid = $insertid;
	if (mysql_num_rows(mysql_query("SELECT * FROM stu_course_link WHERE stu_id='$id' AND course_id='$iid' AND year_link='$yearactive'"))==0)
	{
		mysql_query("INSERT INTO stu_course_link (stu_id, course_id, year_link, semester) VALUES ('$id', '$iid', '$yearactive', '$semesteractive')");
	}
	unset($_SESSION["selected"]);
	if (isset($_SESSION["selecteddep"]))
		unset($_SESSION["selecteddep"]);

}

if (isset($removeid))
{
	$rid = $removeid - 1000;
	mysql_query("DELETE FROM stu_course_link WHERE stu_id='$id' AND course_id='$rid'");
}

if (isset($_POST["search"]))
{
	$search = $_POST["search"];
}
?>
<script language="javascript">
var list = [];
var output = "";
var output2 = "";
var dropdown = "";
var reqdep = [];
var searchbox = "";

// YEAR TABLE:
var yeartable  = "";
	yeartable += "<table><tr>";
	<?
		for ($i=9;$i<=12;$i++)
		{
			$ic = "#AAAAAA";
			if ($i == $yearactive)
				$ic = "#AAAAFF";
			echo "yeartable += '<td bgcolor=\"$ic\" width=20><div onclick=\"document.location=\'?changeyear=$i\';\"><center>$i</center></div></td>';\n";
		
		}
	?>
	yeartable += "</tr></table>";

// SEMESTER TABLE:
var semestertable  = "";
	semestertable += "<table><tr>";
	<?
	for ($i=1;$i<=2;$i++)
	{
		$ic = "#AAAAAA";
		if ($i == $semesteractive)
			$ic = "#AAAAFF";
		echo "semestertable += '<td bgcolor=\"$ic\" width=20><div onclick=\"document.location=\'?changesemester=$i\';\"><center>S$i</center></div></td>';\n";
		
	}
	?>
	semestertable += "</tr></table>";

function makeBubble(id, contents, dep, insert, credit, repeatable, mingrade, maxgrade, prereq, description)
{
	desc = description;
	if (insert==false)
		desc = desc.substring(0,150) + "...";
		
	var o  = "<tr><td>";
        o += "<div id=\""+id+"\" style=\"width:400px;background-color:#FFFFFF;border-style:solid;border-width:1;border-color:#000000;\" onclick=\"pressed(" + id + "," + insert + ");\">";
	    o += "<table width=\"100%\" cellpadding=5 cellspacing=0>";
		o += "<tr><td><b>" + contents + "</b></td><td align=right valign=bottom><font size=1>Credits: " + credit + "&nbsp;&nbsp;&nbsp;Repeatable: " + repeatable + "</font></td></tr>";
		o += "</table>";
		o += "<table cellpadding=2><tr><td><font size=1>" + desc+ "</font></td></tr></table>";
		o += "<table cellpadding=2 width=\"100%\"><tr><td valign=bottom>";
		o += prereq + "</td><td valign=bottom align=right>";
		o += "Grade: " + mingrade + "-" + maxgrade;
		o += "</td></tr></table>";
	    o += "</div>";
	    o += "</td></tr>";
	list[id] = [];
	list[id][0] = contents;
	list[id][1] = dep;
	return o;
}
function pressed(id, insert)
{
	var l = document.getElementById(id);
	var search = "";
	var dp = "";
	<?
		if (isset($search))
		{
			echo "search = 'search=$search&';\n";
		}
		if (isset($dept))
		{
			echo "dp = 'dept=$dept&';\n";
		}
	?>
	if (insert)
		document.location="?"+dp+search+"insertid="+id;
	else
		document.location="?"+dp+search+"removeid="+id;
}
function makeEBubble(id, value, highlight)
{
	var color = "#FFFFFF";
	if (highlight)
		color = "AAAAFF";
		
	var o  = "<tr><td>";
        o += "<div id=\""+id+"\" style=\"width:400px;background-color:"+color+";border-style:solid;border-width:1;border-color:#000000;\" onclick=\"document.location='?selectdep="+value+"';\">";
		o += "<center><h1>" + value + "</h1></center>";
	    o += "</div>";
	    o += "</td></tr>";
	return o;

}
</script>
<?
echo "</HEAD>";
?>
<body>
<?php

echo "<script language=\"javascript\">";
/*
 ************************************
 ******* POPULATE LEFT SIDE**********
 ************************************
 */
if (isset($_SESSION["selected"]))
{
	$esearch = "";
	$semcode = "";
	if ($semesteractive==2)
		$semcode = " AND credit='0.5'";
	if (isset($dept))
		$esearch = "AND dept='$dept'";
	if (isset($search))
		$esearch = "AND (title LIKE '$search%' OR title LIKE '%$search%' OR title like '%$search')";
	$sel = $_SESSION["selected"];
	$q = mysql_query("SELECT seq, Title, dept, credit, repeatable, mingrade, maxgrade, prereq, description FROM JL WHERE SEQ NOT IN (SELECT course_id FROM stu_course_link WHERE stu_id = '$id' AND year_link = '$yearactive') AND (mingrade <= '$yearactive' AND maxgrade >= '$yearactive')  $semcode $esearch AND $sel");
	
	for ($i = 0; $i<mysql_num_rows($q); $i++)
	{
		$name = mysql_result($q, $i, "title");
		$dept = mysql_result($q, $i, "dept");
		$seq = mysql_result($q, $i, "SEQ");
		$credit = mysql_result($q, $i, "credit");
		$repeatable = mysql_result($q, $i, "repeatable");
		$mingrade = mysql_result($q, $i, "mingrade");
		$maxgrade = mysql_result($q, $i, "maxgrade");
		$prereq = mysql_result($q, $i, "prereq");
		$description = mysql_result($q, $i, "description");
		echo "output += makeBubble($seq, \"$name\", \"$dept\", true, \"$credit\", \"$repeatable\", \"$mingrade\", \"$maxgrade\", \"$prereq\", \"$description\");";
	}
	
	if ($_SESSION["selecteddep"]=="Elective")
	{
		$q3 = mysql_query("SELECT DISTINCT dept FROM JL");
		echo "dropdown = \"<select onchange=\\\"document.location='?dept='+this.value;\\\">\";";
		for ($i = 0; $i<mysql_num_rows($q3); $i++)
		{
			$dept = mysql_result($q3, $i, "dept");
			if (isset($sdept)&($dept==$sdept))
				$ins = "SELECTED";
			else
				$ins = "";
			echo "dropdown += '<option value=\"$dept\" $ins>$dept</option>';";
		}
		echo "dropdown += '</select>';";
		
		echo 'searchbox += \'<input name="search" type="text" id="search"><input type="submit"></form>\';';
	}
}
$q2 = mysql_query("SELECT seq, Title, dept, credit, repeatable, mingrade, maxgrade, prereq, description FROM JL WHERE SEQ IN (SELECT course_id FROM stu_course_link WHERE stu_id = '$id' AND year_link = '$yearactive')");
$tdept = "";
//$q2 = mysql_query("SELECT * FROM JL JOIN stu_course_link ON JL.seq = stu_course_link.course_id WHERE year_link = '$yearactive' AND (semester='$semesteractive' OR credit='1')");
$missingdep = mysql_query("SELECT DISTINCT dep FROM requirements WHERE dep NOT IN (SELECT dep FROM requirements WHERE year='$yearactive' AND course_id IN (SELECT course_id FROM stu_course_link WHERE stu_id = '$id' AND year_link = '$yearactive'))");
for ($i = 0; $i<mysql_num_rows($q2); $i++)
{
	$name = mysql_result($q2, $i, "title");
	$dept = mysql_result($q2, $i, "dept");
	$seq = mysql_result($q2, $i, "SEQ");
	$credit = mysql_result($q2, $i, "credit");
	$repeatable = mysql_result($q2, $i, "repeatable");
	$mingrade = mysql_result($q2, $i, "mingrade");
	$maxgrade = mysql_result($q2, $i, "maxgrade");
	$prereq = mysql_result($q2, $i, "prereq");
	$description = mysql_result($q2, $i, "description");
	echo "output2 += makeBubble($seq+1000, \"$name\", \"$dept\", false, \"$credit\", \"$repeatable\", \"$mingrade\", \"$maxgrade\", \"$prereq\", \"$description\");";

}
for ($i = 0; $i<mysql_num_rows($missingdep); $i++)
{
	$req = mysql_result($missingdep, $i, "dep");
	$val = "false";
	if (isset($_SESSION["selecteddep"]))
	{
		if ($_SESSION["selecteddep"]==$req)
		{
			$val = "true";
		}
	} 
	echo "output2 += makeEBubble(999, \"$req\", $val);";
	
}
for ($i = 0; $i<8-(mysql_num_rows($missingdep)+mysql_num_rows($q2)); $i++)
{
	$val = "false";
	if (isset($_SESSION["selecteddep"]))
	{
		if ($_SESSION["selecteddep"]=="Elective")
		{
			$val = "true";
		}
	}
	echo "output2 += makeEBubble(998, \"Elective\", $val);";
}



?>
document.write('<div style="position:absolute;top:60px;left:5%;">');
document.write("<table style=\"background-color:#FFFFFF;\" cellpadding=1 cellspacing=1><table cellpadding=0 cellspacing=0><tr><td width=352>"+yeartable + "</td><td>" +semestertable +"</td></tr></table>" +output2 + "</table>");
document.write('</div>');
document.write('<div id="mainbox" style="position:absolute;top:60px;left:55%;">');
document.write(dropdown);
document.write('<form action="?" method="post">');
document.write(searchbox + "<table style=\"background-color:#FFFFFF;\" cellpadding=1 cellspacing=1>"+output+"</table>");
document.write('</div>');
<?
echo "</script>";
?>

</body></html>